package com.example.novelsys.controller;

import com.example.novelsys.entity.Categories;
import com.example.novelsys.entity.Novel;
import com.example.novelsys.service.CategoriesService;
import com.example.novelsys.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class SearchNovelController {
    @Autowired
    CategoriesService categoriesService;
    @Autowired
    NovelService novelService;

    //------------------------------------------------------根据书名或 作者 模糊查询小说
    @RequestMapping("/seachNovle")
    public String searchNovelKey(String searchKey, HttpServletRequest req) {

        //获取搜索的小说
        List<Novel> searchNovel = novelService.queryLike(searchKey);

        //4、获取下载排行小说
        List<Novel> downList = novelService.queryNovelByType(4);
        //5、获取阅读排行小说
        List<Novel> readList = novelService.queryNovelByType(5);

        List rankInfo = new ArrayList();
        rankInfo.add(downList);
        rankInfo.add(readList);

        req.setAttribute("searchNovel", searchNovel);
        req.setAttribute("rankInfo", rankInfo);

        //获取小说类别
        List<Categories> categoriesList=categoriesService.queryAll();
        req.setAttribute("categoriesList", categoriesList); //单独用于用于菜单栏显示

        return "seachNovleList";
    }


    //------------------------------------------------------根据类型查询小说
    @RequestMapping("/toCategory")
    public String searchNovelType(Integer cateId,Integer orderIndex, Integer fileIndex, HttpServletRequest req){
        //排序方式 或者 文件大小 选中标志
        List selectIndex=new ArrayList<Integer>();
        //默认排序方式 或者 文件大小都没有选中
        selectIndex.add(-1);
        selectIndex.add(-1);

        if (orderIndex != null && !"".equals(orderIndex)) {

            selectIndex.set(0, orderIndex);

        }

        if (fileIndex != null && !"".equals(fileIndex)) {
            selectIndex.set(1, fileIndex);
        }



        // 获取该类型的小说
        List<Novel> novelCateList = novelService.queryByCateId(cateId, orderIndex,fileIndex);

        req.setAttribute("novelCateList", novelCateList);
        req.setAttribute("selectIndex", selectIndex);

        //获取小说类别
        List<Categories> categoriesList=categoriesService.queryAll();
        req.setAttribute("categoriesList", categoriesList); //单独用于用于菜单栏显示

        return "categoryList";
    }
}
